package com.aaa.common.service.impl;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.entity.DictType;
import com.aaa.common.mapper.DictTypeMapper;
import com.aaa.common.service.DictTypeService;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 字典类型表(DictType)表服务实现类
 *
 * @author makejava
 * @since 2020-11-16 21:06:58
 */
@Service("dictTypeService")
public class DictTypeServiceImpl implements DictTypeService {
    @Resource
    private DictTypeMapper dictTypeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param dictId 主键
     * @return 实例对象
     */
    @Override
    public Result queryById(Integer dictId) {
        if (dictId == null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(), ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.dictTypeMapper.queryById(dictId));
    }



    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return Result --- Result.getData():对象列表
     */
    @Override
    public Result queryAllByLimit(Integer offset, Integer limit) {
        if (offset == null || limit == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.dictTypeMapper.queryAllByLimit(offset, limit));
    }
    /**
     * 查询指定行数据
     *
     * @param pageNo   查询起始位置
     * @param pageSize 查询条数
     * @param dictType     查询条数
     * @return Result ---- Result.getData() 对象列表
     */
    @Override
    public Result queryAllByParam(Integer pageNo, Integer pageSize, DictType dictType) {
        if (pageNo == 0 || pageSize == 0) {
            throw  new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(),
                    ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        PageHelper.startPage(pageNo, pageSize);
        PageInfo<DictType> pageInfo = new PageInfo<>(dictTypeMapper.queryAll(dictType));
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), pageInfo);
    }

    /**
     * 新增数据
     *
     * @param dictType 实例对象
     * @return 实例对象
     */
    @Override
    public Result insert(DictType dictType) {
        if (dictType == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        this.dictTypeMapper.insert(dictType);
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), dictType);
    }

    /**
     * 修改数据
     *
     * @param dictType 实例对象
     * @return 实例对象
     */
    @Override
    public Result update(DictType dictType) {
        if (dictType == null) {
            throw new CustomException(ExceptionConstant.INVALID_ARGUMENT.getErrorCode(), ExceptionConstant.INVALID_ARGUMENT.getErrorMessage());
        }
        this.dictTypeMapper.update(dictType);
        Integer dictid = (Integer) dictType.getDictId();
        return this.queryById(dictid);
    }

    /**
     * 通过主键删除数据
     *
     * @param dictId 主键
     * @return 是否成功
     */
    @Override
    public Result deleteById(Integer dictId) {
        if (dictId == null) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(), ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result<>(ReturnCode.SUCCESS.getCode(), ReturnCode.SUCCESS.getMsg(), this.dictTypeMapper.deleteById(dictId) > 0);
    }
}